home *** CD-ROM | disk | FTP | other *** search
- /* File: Windows.c */
-
- #include "ScriptScrap.h"
- #include "Prototypes.h"
-
- #include "AERegistry.h"
-
-
- /* This routine gets called whenever the current selection gets changed and we */
- /* need to draw a new entry or change the status area at the bottom of the window. */
- /* We'll use it to selectively invalidate only those areas which need re-drawing, */
- /* which will prevent the "frame" portion from flickering. */
- void InvalidateWindow(WindowPtr whichWindow)
- {
- Rect r;
- wiHand info = (wiHand)GetWRefCon(whichWindow);
-
- SetPort(whichWindow);
- r = (*info)->drawingArea;
- InvalRect(&r);
- r = (*info)->statusArea;
- InvalRect(&r);
- r = (*(*info)->selectionBar)->contrlRect;
- InvalRect(&r);
- }
-
-
- void EntryAdded (WindowPtr whichWindow, short entryNum)
- {
- /* This routine gets called whenever a new entry gets added to the */
- /* scrapbook, and it updates the display appropriately. */
- wiHand info = (wiHand)GetWRefCon(whichWindow);
-
- (*info)->numEntries += 1;
- HiliteControl((*info)->selectionBar, 0);
- SetCtlMax((*info)->selectionBar, (*info)->numEntries);
- InvalidateWindow(whichWindow);
- }
-
-
- void EntryRemoved (WindowPtr whichWindow, short entryNum)
- {
- /* This routine gets called whenever an entry is removed from the */
- /* scrapbook, and it updates the display appropriately. */
- wiHand info = (wiHand)GetWRefCon(whichWindow);
-
- if ((*info)->numEntries > 0)
- (*info)->numEntries -= 1;
- if ((*info)->numEntries == 0)
- HiliteControl((*info)->selectionBar, 255);
- SetCtlMax((*info)->selectionBar, (*info)->numEntries);
- if ((*info)->currEntryNum > (*info)->numEntries)
- GoToEntry(whichWindow, (*info)->numEntries);
- else
- InvalidateWindow(whichWindow);
- }
-
-
- void GoToEntry (WindowPtr whichWindow, short entryNum)
- {
- wiHand info = (wiHand)GetWRefCon(whichWindow);
- ResType scratchType;
- OSErr err;
-
- if (entryNum < 1)
- entryNum = 1;
- else if (entryNum > (*info)->numEntries)
- entryNum = (*info)->numEntries;
- err = GetBestType(entryNum, &scratchType); /* Get the default type for this entry */
- if (err == noErr)
- (*info)->currEntryType = scratchType;
- SetCtlValue((*info)->selectionBar, entryNum);
- (*info)->currEntryNum = entryNum;
- InvalidateWindow(whichWindow);
- DoUpdate(whichWindow);
- }
-
-
- OSErr NewScrapbookFile(FSSpec *theFile, Boolean *cancelled)
- {
- /* This routine asks the user to create a new scrapbook file. If a file already */
- /* exists, it replaced. */
- /* Returns noErr if successful, and places an FSSpec into *theFile. */
- /* Returns errAEEventFailed if the user cancels, and sets *cancelled to TRUE. */
- /* Otherwise, returns any intermediate error codes that may occur. */
-
- smapHand theSmap;
- StandardFileReply reply;
- OSErr err;
- short refNum = 0;
-
- err = MyInteractWithUser(true); /* Make sure we're in front */
- if (err != noErr) goto done;
-
- StandardPutFile("\p", "\pNew Scrapbook", &reply);
- *cancelled = !reply.sfGood;
-
- if (reply.sfGood) {
- *theFile = reply.sfFile;
- /* Create the file so we can open it */
- if (reply.sfReplacing) /* Get rid of the old file first */
- if (err = FSpDelete(theFile)) goto done;
- FSpCreateResFile(theFile, 'scbk', 'scbk', reply.sfScript);
- /* Open the file and create a blank SMAP resource in it */
- refNum = FSpOpenResFile(theFile, fsRdWrPerm);
- if ((err = ResError()) != noErr) goto done;
- UseResFile(refNum);
- theSmap = (void *) NewHandleClear(sizeof(smap));
- if ((err = MemError()) != noErr) goto done;
- AddResource((Handle)theSmap, 'SMAP', 0, "\p");
- if ((err = ResError()) != noErr) goto done;
- UpdateResFile(refNum);
- if ((err = ResError()) != noErr) goto done;
-
- } else
- err = errAEEventFailed; /* The user cancelled */
-
- done:
- if (refNum > 0)
- CloseResFile(refNum);
-
- return err;
- }
-
-
- OSErr NewDisplayWindow (const FSSpec *sourceFile)
- {
- FSSpec fileToOpen;
- WindowPtr newWindow = NULL;
- wiHand info = NULL;
- Ptr storage = NULL;
- register OSErr err = noErr;
- short refNum = 0;
- short numEntries;
- Rect scrollBarBounds,
- drawingArea, statusArea;
- ControlHandle scrollBar;
- ResType scratchType;
- Boolean cancelled;
-
- if (sourceFile == NULL) {
- /* If we haven't been given a file, then ask the user for one */
- err = NewScrapbookFile(&fileToOpen, &cancelled);
- if (err != noErr) goto error;
- } else {
- /* The caller specified a file */
- fileToOpen = *sourceFile;
- }
- /* Open the file */
- refNum = FSpOpenResFile(&fileToOpen, fsRdWrPerm);
- if ((err = ResError()) != noErr) goto error;
- UseResFile(refNum);
-
- /* Create a new window */
- storage = NewPtrClear(sizeof(WindowRecord));
- if (err = MemError()) goto error;
- newWindow = GetNewCWindow(kDisplayWindow, storage, (WindowPtr)-1);
- if (err = MemError()) goto error;
- SetPort(newWindow);
-
- /* Create and initialize our window information block */
- info = (wiHand)NewHandleClear(sizeof(WindowInfo));
- if (err = MemError()) goto error;
-
- /* Define the area where each entry will be drawn */
- drawingArea = newWindow->portRect;
- drawingArea.bottom = drawingArea.bottom - 48;
- InsetRect(&drawingArea, 11, 11);
- (*info)->drawingArea = drawingArea;
-
- /* Define the area where the curr entry number, # of entries, */
- /* and entry types will be drawn. */
- statusArea = newWindow->portRect;
- statusArea.top = statusArea.bottom - 32;
- statusArea.bottom -= 6;
- (*info)->statusArea = statusArea;
-
- (*info)->fRefNum = refNum;
- (*info)->fileSpec = fileToOpen;
- err = CountScrapbookEntries(&numEntries);
- if (err != noErr) goto error;
-
- /* Construct the scroll bar */
- SetRect(&scrollBarBounds, 5, newWindow->portRect.bottom - 40, newWindow->portRect.right - 5, newWindow->portRect.bottom - 24);
- /* Create a scroll bar with a minimum value of 1 and a max. value of numEntries */
- /* If numEntries is 0, this would present a problem, except that the update */
- /* routine disables the scroll bar if numEntries == 0. */
- scrollBar = NewControl(newWindow, &scrollBarBounds, "\p", true, 1, 1, numEntries, scrollBarProc, 0);
- (*info)->selectionBar = scrollBar;
- (*info)->numEntries = numEntries;
- if (numEntries > 0) {
- (*info)->currEntryNum = 1; /* Select entry # 1 */
- err = GetBestType(1, &scratchType); /* Get the default type for entry 1 */
- if (err != noErr) goto error;
- (*info)->currEntryType = scratchType;
- }
-
- /* Everything worked, so set up the window and display it */
- SetWRefCon(newWindow, (long)info);
- SetWTitle(newWindow, fileToOpen.name);
- ShowWindow(newWindow);
- DoUpdate(newWindow);
- goto success;
-
- error:
- /* If there was a problem, then dispose of any allocated memory and close the file */
- if (refNum > 0)
- CloseResFile(refNum);
- if (info != NULL)
- DisposHandle((Handle)info);
- if (newWindow != NULL)
- CloseWindow(newWindow);
- if (storage != NULL)
- DisposPtr(storage);
-
- success:
- return err;
- } /* NewDisplayWindow */
-
-
- void CloseAWindow (WindowPtr whichWindow)
- {
- short wKind;
- wiHand info;
-
- if (whichWindow) {
- wKind = ((WindowPeek)whichWindow)->windowKind;
- if (wKind < 0)
- CloseDeskAcc(wKind);
- else if (wKind == userKind) {
- info = (wiHand)GetWRefCon(whichWindow);
- CloseResFile((*info)->fRefNum);
- DisposHandle((Handle)info);
- DisposeWindow(whichWindow);
- AdjustMenus;
- }
- }
- } /* CloseAWindow */
-
-
- /*----------------------- Handle window update events ----------------------*/
-
-
- void DoUpdate (WindowPtr wp)
- {
- Rect updateRect;
- wiHand info = (wiHand)GetWRefCon(wp);
- Rect portRect = wp->portRect;
- Rect picRect;
- RgnHandle oldClip = NULL;
- Rect drawingArea, statusArea, drawingFrame, picFrame, scaledFrame;
- ResType resTypeList[10];
- short numResTypes, count;
- OSErr err;
- Str255 scratchStr;
- AEDesc currItemDesc;
-
- SetPort(wp); /* make update window active grafPort */
- BeginUpdate(wp); /* visRgn temporarily = updateRgn */
- EraseRect(&portRect);
- if ((*info)->numEntries == 0)
- HiliteControl((*info)->selectionBar, 255);
- else
- HiliteControl((*info)->selectionBar, 0);
- DrawControls(wp);
-
- /* Get the areas of our window from the info record */
- drawingArea = (*info)->drawingArea;
- statusArea = (*info)->statusArea;
-
- /* Draw the nested boxs which bound the currently displayed entry */
- PenNormal();
- ForeColor(blackColor);
- drawingFrame = drawingArea;
- InsetRect(&drawingFrame, -1, -1);
- PenSize(1, 1);
- FrameRect(&drawingFrame);
- InsetRect(&drawingFrame, -5, -5);
- /* Draw the large frame in gray if we're de-activated */
- if (((WindowPeek)wp)->hilited == 0)
- PenPat(qd.gray);
- ForeColor(blueColor);
- PenSize(3, 3);
- FrameRect(&drawingFrame);
- ForeColor(blackColor);
- PenNormal();
-
- /* remember our old clipping region so we can set the clip before drawing */
- /* certain items */
- oldClip = NewRgn();
- GetClip(oldClip);
-
-
- /* Draw the "number of items" statistic */
- ClipRect(&statusArea);
- TextFont(monaco); TextSize(9); TextFace(0);
- MoveTo(statusArea.left, statusArea.bottom - 2);
- NumToString((*info)->currEntryNum, scratchStr);
- DrawString(scratchStr);
- DrawString("\p / ");
- NumToString((*info)->numEntries, scratchStr);
- DrawString(scratchStr);
-
- /* Draw the list of resource types */
- numResTypes = 10;
- err = GetAllTypes((*info)->currEntryNum, &numResTypes, resTypeList);
- if (err == noErr) {
- /* Draw the list of types */
- MoveTo(statusArea.right - numResTypes * StringWidth("\p ") - 5,
- statusArea.bottom - 2);
- for (count = 0; count < numResTypes; count++) {
- OSTypeToPStr(resTypeList[count], (char *)scratchStr);
- DrawString(scratchStr);
- if (count < numResTypes - 1)
- DrawString("\p, ");
- }
- }
-
-
- /* Draw the currently selected scrapbook entry */
- ClipRect(&drawingArea);
- err = Get1ScrapbookItem ((*info)->currEntryNum, (*info)->currEntryType, &currItemDesc);
- if (err == noErr) {
- if (currItemDesc.descriptorType == 'PICT') {
- picFrame = (*(PicHandle)currItemDesc.dataHandle)->picFrame;
- // <<< Create a properly scaled frame here
- DrawPicture((PicHandle)currItemDesc.dataHandle, &drawingArea);
- }
- else if (currItemDesc.descriptorType == 'TEXT') {
- MoveHHi(currItemDesc.dataHandle);
- HLock(currItemDesc.dataHandle);
- TextFont(times);
- TextSize(0);
- TextFace(0);
- TextBox(*currItemDesc.dataHandle, GetHandleSize(currItemDesc.dataHandle), &drawingArea, teJustLeft);
- }
- else {
- /* We don't know how to draw it */
- MoveTo(drawingArea.left + 2, drawingArea.top + 18);
- TextSize(12); TextFont(times); TextFace(bold);
- DrawString("\pCannot display this entry");
- }
- (void) AEDisposeDesc(&currItemDesc);
- }
-
- SetClip(oldClip);
- DisposeRgn(oldClip);
-
- DrawControls(wp);
- EndUpdate(wp); /* restore normal visRgn of grafport */
- } /* DoUpdate */
-
-
- /* This is called whenever a window is activated or deactivated and is responsible */
- /* for highlighting (or dimming) the contents of the window appropriately. */
- void DoActivate (WindowPtr wp, Boolean activate)
- {
- wiHand info = (wiHand)GetWRefCon(wp);
-
- AdjustMenus();
- SetPort(wp);
- /* Highlight the selection bar appropriately */
- if (activate && ((*info)->numEntries > 0))
- HiliteControl((*info)->selectionBar, 0);
- else
- HiliteControl((*info)->selectionBar, 255);
- /* Redraw the window so that the drawing area frame can be grayed out */
- /* or highlighted. */
- InvalRect(&wp->portRect);
- DoUpdate(wp);
- } /* DoActivate */
-
-
- static pascal void ControlAction(ControlHandle whichControl, short part);
- /* Here's an action procedure which handles the case where the user */
- /* holds the mouse down in the selection scrollbar */
-
- static pascal void ControlAction(ControlHandle whichControl, short part)
- {
- short value = GetCtlValue(whichControl);
-
- switch(part) {
- case inUpButton:
- value -= 1;
- break;
-
- case inDownButton:
- value += 1;
- break;
-
- case inPageUp:
- value -= 5;
- break;
-
- case inPageDown:
- value += 5;
- break;
-
- default:
- return;
- }
-
- GoToEntry((*whichControl)->contrlOwner, value);
-
- }
-
-
- void DoContentClick (WindowPtr whichWindow, Point globalPt)
- {
- Point localPt;
- ControlHandle whichControl;
- short part;
- wiHand info;
-
- if (whichWindow != FrontWindow())
- SelectWindow(whichWindow);
- else {
- SetPort(whichWindow);
- localPt = globalPt;
- GlobalToLocal(&localPt);
- info = (wiHand)GetWRefCon(whichWindow);
-
- /* The only clicks that interest us are clicks in the selection scrollbar */
- if (part = FindControl(localPt, whichWindow, &whichControl)) {
- if (part == inThumb) {
- TrackControl(whichControl, localPt, NULL);
- GoToEntry(whichWindow, GetCtlValue(whichControl));
- } else
- TrackControl(whichControl, localPt, ControlAction);
-
- }
-
- }
- } /* DoContentClick */
-
-